/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.app.controller.system;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysConf;
import com.easy.module.system.query.SysConfQuery;
import com.easy.module.system.service.ISysConfService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置表 控制器
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/conf")
public class SysConfController extends BaseController {

    private final ISysConfService confService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@perm.hasPermission('system:conf$list')")
    @GetMapping("/page")
    public R<Pagination<SysConf>> page(SysConfQuery query) {
        return R.success(confService.page(query));
    }

    @Log(title = "参数管理", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:conf$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfQuery query) throws Exception {
        List<SysConf> list = confService.list(query);
        ExcelUtils.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@perm.hasPermission('system:conf$query')")
    @GetMapping(value = "/{id}")
    public R<SysConf> get(@PathVariable("id") Long id) {
        return R.success(confService.getById(id));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/key/{key}")
    public R getConfKey(@PathVariable("key") String key) {
        return R.success(confService.getValue(key));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@perm.hasPermission('system:conf$add')")
    @Log(title = "参数管理", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysConf conf) {
        if (confService.existByKey(conf)) {
            return R.fail("新增参数'" + conf.getName() + "'失败，参数键名已存在");
        }
        return R.result(confService.add(conf));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@perm.hasPermission('system:conf$edit')")
    @Log(title = "参数管理", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysConf conf) {
        if (confService.existByKey(conf)) {
            return R.fail("修改参数'" + conf.getName() + "'失败，参数键名已存在");
        }
        return R.result(confService.update(conf));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@perm.hasPermission('system:conf$remove')")
    @Log(title = "参数管理", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids) {
        confService.deleteByIds(ids);
        return R.success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@perm.hasPermission('system:conf$remove')")
    @Log(title = "参数管理", type = Operation.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        confService.resetConfCache();
        return R.success();
    }
}
